﻿using System.Collections.Generic;
using System.Web.Mvc;
using ControleRepresentante.Logic;
using ControleRepresentante.Logic.Models;
using ControleRepresentante.Logic.Repositories;
using ControleRepresentante.Logic.Repositories.SearchExtensions;
using WebSite.ViewModels.ViewModelExtensions.DetailsViewModelExtensions;
using WebSite.ViewModels.ViewModelExtensions.EditViewModelExtensions;
using WebSite.ViewModels.ViewModelExtensions.IndexViewModelExtensions;

namespace WebSite.Controllers.ControllerExtensions
{
    public abstract class ControllerExtensionBase<TModel> : IControllerExtension
        where TModel : ModelBase
    {
        public IIndexViewModelExtension GetIndexViewModelExtension(FormCollection formCollection, IRepository repository)
        {
            return DoGetIndexViewModelExtension(formCollection ?? new FormCollection(), repository);
        }

        protected abstract IIndexViewModelExtension DoGetIndexViewModelExtension(FormCollection formCollection, IRepository repository);
        public abstract IEditViewModelExtension EditViewModelExtension { get; }
        public abstract IDetailsViewModelExtension DetailsViewModelExtension { get; }
        protected abstract Area Area { get; }
        public abstract ISearchExtension GetSearchExtension(FormCollection formCollection);
        protected abstract Dictionary<string, string> TryUpdateModel(TModel model, FormCollection formCollection);
        protected abstract void SetModel(TModel model);
        protected abstract bool CanHandle(Representante representante);

        public bool CanHandle(Area area, Representante representante)
        {
            return area == Area && CanHandle(representante);
        }

        public Dictionary<string, string> TryUpdateModel(ModelBase model, FormCollection formCollection)
        {
            TModel castedModel = model as TModel;
            SetModel(castedModel);
            return TryUpdateModel(castedModel, formCollection ?? new FormCollection());
        }

        public void SetModel(ModelBase model)
        {
            SetModel((TModel) model);
        }

        public abstract void RegisterIndexColumns(IIndexColumnsProvider indexColumnsProvider);
    }
}